package com.daisyday.manage.auth.service;


import com.daisyday.manage.auth.dao.db1.UserInfoDao;
import com.daisyday.manage.auth.entry.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 希望用户的信息来自数据库，而不是写死的，所以我们就需要实现UserDetailsService接口
 */
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {


    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo=userInfoDao.selectByUserName(username);
        if(userInfo==null){
            throw new UsernameNotFoundException("username or password error");
        }
        String[] permissionArray = new String[1];
        permissionArray[0]="admin";
        // 将用户名称/用户密码以及用户拥有的权限放入UserDetails中
        UserDetails userDetails = User.withUsername(username)
                                      .password(userInfo.getPassword())
                                      .authorities(permissionArray)
                                      .build();
        return userDetails;
    }


}
